package org.itboys.weixin.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.weixin.entity.WeixinMessageKeyword;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

/**
 * 微信消息相关
 * @author ChenJunhui
 *
 */
@Service
public class WeixinMessageKeywordService extends BaseWeixinService<WeixinMessageKeyword, Long> {

	private static final long serialVersionUID = 5461856131177338967L;
	
	@Resource(name="weixinDS")
	private MongoDataSource keywordDataSource;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return keywordDataSource;
	}

	@Override
	protected Class<WeixinMessageKeyword> getEntityClass() {
		return WeixinMessageKeyword.class;
	}
	
	public void save(WeixinMessageKeyword detail){
		detail.setCreateTime(new Date());
		detail.setCreator(AdminSessionHolder.getAdminUserId());
		super.save(detail);
	}
	
	public void update(WeixinMessageKeyword detail){
		WeixinMessageKeyword detail2 = getById(detail.getId());
		detail.setCreateTime(detail2.getCreateTime());
		detail.setCreator(detail2.getCreator());
		detail.setUpdater(AdminSessionHolder.getAdminUserId());
		super.update(detail);
	}
	
	public Long getMsgIdByKeywordAndWeixinId(String keyword,Long weixinId){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("weixinId", weixinId);
		param.put("keyword", keyword);
		List<WeixinMessageKeyword> list = list(param);
		return list!=null&&!list.isEmpty()?list.get(0).getId():null;
	}
	
}
